package thing

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"iot-base/auth/api/logic"
	Log "iot-base/auth/logger"
	"iot-base/auth/types"
	"iot-base/auth/utils"
	caliType "iot-base/common/types"
	"strings"
)

func GetEdgeId(stationId string) string {
	return logic.GetGasStationEdgeId(stationId)
}

func CmdZero(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqCalibratorZero
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	stationLogic := logic.NewStationLogic()
	thingId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, req.ModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	//校准仪
	caliId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, types.CaloModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	seqData := caliType.ReqZeroCali{
		RequestId: requestId.(string),
		StationId: req.StationId,
		ThingId:   thingId,
		CaliId:    caliId,
		ModelId:   req.ModelId,
		Event:     req.Event,
	}
	_, err = utils.SendEdgeCmd("ZeroCalibrate", GetEdgeId(req.StationId), seqData)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CmdSpan(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqCalibratorSpan
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	req.Units = strings.ToUpper(req.Units)
	stationLogic := logic.NewStationLogic()
	thingId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, req.ModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	//校准仪
	caliId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, types.CaloModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	seqData := caliType.ReqSpanCali{
		RequestId: requestId.(string),
		StationId: req.StationId,
		ThingId:   thingId,
		CaliId:    caliId,
		ModelId:   req.ModelId,
		Event:     req.Event,
		Number:    req.Number,
		Units:     req.Units,
	}
	_, err = utils.SendEdgeCmd("SpanCalibrate", GetEdgeId(req.StationId), seqData)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CmdGetStatus(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqDevice
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	seqData := caliType.ReqGetStatus{
		StationId: req.StationId,
	}
	result, err := utils.SendEdgeCmd("GetZeroOrSpanStatus", GetEdgeId(req.StationId), seqData)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	var retMap map[string]any
	_ = json.Unmarshal(result, &retMap)
	resp.Success(retMap)
	return
}

func CmdClearStatus(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqDevice
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	seqData := caliType.ReqGetStatus{
		StationId: req.StationId,
	}
	_, err := utils.SendEdgeCmd("ResetZeroOrSpanStatus", GetEdgeId(req.StationId), seqData)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CmdCaliTask(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqCaliTask
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, err.Error())
		return
	}
	if !checkCliTaskType(req.TaskType) {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	stationLogic := logic.NewStationLogic()
	thingId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, req.ModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	//校准仪
	caliId, err := stationLogic.GetThingIdByStationIdAndModelId(req.StationId, types.CaloModelId)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	seqData := caliType.ReqCaliTask{
		RequestId: requestId.(string),
		StationId: req.StationId,
		ThingId:   thingId,
		CaliId:    caliId,
		ModelId:   req.ModelId,
		TaskType:  req.TaskType,
	}
	_, err = utils.SendEdgeCmd("CaliTask", GetEdgeId(req.StationId), seqData)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CmdStopCaliTask(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)
	var req types.ReqStopCaliTask
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	_, err := utils.SendEdgeCmd("StopCaliTask", GetEdgeId(req.StationId), []byte{})
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CmdQueryCaliTaskStatus(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types.NewResult(c)

	var req types.ReqQueryCaliTaskStatus
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}

	result, err := utils.SendEdgeCmd("QueryRunTask", GetEdgeId(req.StationId), []byte{})
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	var retMap map[string]any
	_ = json.Unmarshal(result, &retMap)
	resp.Success(retMap)
	return
}
